package cn.net.xinyi.xs.util;

import java.text.NumberFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	public static String convertToDoubleDigitNumber(int number) {
		// ���ܸ�ʽ�������ַ�
		String showNumber = null;

		// ���ָ�ʽ��
		NumberFormat numberFormat = NumberFormat.getIntegerInstance();

		// ��ʽ��ǿ��2λ������λ��0
		numberFormat.setMaximumIntegerDigits(2);
		numberFormat.setMinimumIntegerDigits(2);

		// ִ�и�ʽ��������ʽ�������ַ�
		showNumber = numberFormat.format(number);

		// �����ַ�
		return showNumber;
	}

	// ��ȡ�Ϸ���xml�ַ�
	public static String toLegalXMLString(String str) {
		if (str != null && str.trim().length() != 0) {
			str = str.replaceAll("&", "&amp;");
			str = str.replaceAll("<", "&lt;");
			str = str.replaceAll(">", "&gt;");
			str = str.replaceAll("'", "&apos;");
			str = str.replaceAll("\"", "&quot;");
		} else {
			str = "";
		}
		return str;
	}

	// ��ȡ�Ϸ���javascript�ַ�
	public static String toLegalJSString(String str) {
		// ��(' " )--->(' " \)����js�е�
		char strchar[] = str.toCharArray();
		String strstr = "";
		if (str != null && str.trim().length() != 0) {
			for (int i = 0; i < str.length(); i++) {
				if (Character.toString(strchar[i]).equals("'")) {
					strstr += "\'";
				} else if (Character.toString(strchar[i]).equals("\"")) {
					strstr += "\"";
				} else if (Character.toString(strchar[i]).equals("\\")) {
					strstr += "\\";
				} else {
					strstr += Character.toString(strchar[i]);
				}
			}
		} else {
			strstr = "";
		}
		return strstr;
	}

	public static String trimSQL(String sql, String endDelimiter) {
		sql = sql.trim();
		if (sql.endsWith(endDelimiter)) {
			sql = sql.substring(0, sql.length() - 1 - endDelimiter.length());
		}
		return sql;
	}

	public static String htmltoText(String inputString) {
		String htmlStr = inputString; // ��html��ǩ���ַ�
		String textStr = "";
		Pattern p_script;
		Matcher m_script;
		Pattern p_style;
		Matcher m_style;
		Pattern p_html;
		Matcher m_html;
		Pattern p_ba;
		Matcher m_ba;

		try {
			String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // ����script��������ʽ{��<script[^>]*?>[\\s\\S]*?<\\/script>
			// }
			String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // ����style��������ʽ{��<style[^>]*?>[\\s\\S]*?<\\/style>
			// }
			String regEx_html = "<[^>]+>"; // ����HTML��ǩ��������ʽ
			String patternStr = "\\s+";

			p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
			m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll(""); // ����script��ǩ

			p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
			m_style = p_style.matcher(htmlStr);
			htmlStr = m_style.replaceAll(""); // ����style��ǩ

			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll(""); // ����html��ǩ

			p_ba = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
			m_ba = p_ba.matcher(htmlStr);
			htmlStr = m_ba.replaceAll(""); // ���˿ո�

			textStr = htmlStr;

		} catch (Exception e) {
			System.err.println("Html2Text: " + e.getMessage());
		}
		return textStr;// �����ı��ַ�
	}

	public static boolean isCHcharacters(String inputString) {
		String str = inputString;
		Pattern p_CH;
		Matcher m_CH;
		String regEx_CH = "[\u4e00-\u9fa5]";
		p_CH = Pattern.compile(regEx_CH, Pattern.CASE_INSENSITIVE);
		m_CH = p_CH.matcher(str);
		return m_CH.find();
	}

	public static String removeAllBlankSpace(String inputString) {
		String str = inputString;
		Pattern p_CH;
		Matcher m_CH;
		String regEx_CH = "[\\s\\p{Zs}]";
		p_CH = Pattern.compile(regEx_CH, Pattern.CASE_INSENSITIVE);
		m_CH = p_CH.matcher(str);
		str = m_CH.replaceAll("");
		return str;
	}

//	public static void main(String[] args) {
//		String s = "1, 2, 8, 90 , 988";
//		Pattern pattern = Pattern.compile("[\\s\\p{Zs}]");
//		Matcher re = pattern.matcher(s);
//		System.out.println(re.replaceAll(""));
//	}
}
